package com.example.demo2.common.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @Author 胡亚伟
 * @Date 2025-06-101
 * @version 1.0
 * @注释
 */
public class Strings2 {

    /**
     * 首字母大写
     *
     * @param string
     * @return
     */
    public static String toUpperCase4Index(String string) {
        char[] methodName = string.toCharArray();
        methodName[0] = toUpperCase(methodName[0]);
        return String.valueOf(methodName);
    }

    /**
     * 字符转成大写
     *
     * @param chars
     * @return
     */
    public static char toUpperCase(char chars) {
        if (97 <= chars && chars <= 122) {
            chars ^= 32;
        }
        return chars;
    }

    public static String hiddenTel(String tel) {
        if (tel == null) {
            return null;
        }
        return tel.replaceFirst("(?<=\\d{3})\\d{4}(?=\\d{4})", "****");
    }

    /**
     * Check whether given string is null, empty or white space.
     *
     * @param value The string to be checked.
     * @return <code>true</code> if <code>value</code> is null, empty, or white
     *         space; Otherwise <code>false</code>.
     */
    public static boolean isNullOrWhiteSpace(String value) {
        if (StringUtils.isNotBlank(value)) {
            return false;
        }
        return true;
    }

    public static boolean isBooleanValue(String value) {
        if (StrUtil.isEmpty(StrUtil.trimToEmpty(value))) {
            return false;
        }
        if (Objects.equal(value, "true") || Objects.equal(value, "false")) {
            return true;
        }
        return false;
    }

    public static int parseInt(String str, int defaultValue) {
        if (Strings.isNullOrEmpty(str)) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static long parseLong(String str, long defaultValue) {
        if (Strings.isNullOrEmpty(str)) {
            return defaultValue;
        }
        try {
            return Long.parseLong(str);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static String toLowerCase(String str) {
        if (str == null) {
            return null;
        }
        return str.toLowerCase();
    }

    public static String trim(String str) {
        return str != null ? str.trim() : null;
    }

    public static String SHA1(String message) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA1");
            byte[] result = digest.digest(message.getBytes());
            StringBuffer sb = new StringBuffer();
            for (byte b : result) {
                sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }

    public static String substring(String chars, int maxLength) {
        if (Strings2.isNullOrWhiteSpace(chars)) {
            return null;
        }
        int charsLength = 0;
        for (int i = 0; i < chars.length(); i++) {
            String charName = chars.substring(i, i + 1);
            // 判断中文 [\u4e00-\u9fa5]
            // 判断双字节字符 [^\\x00-\\xff]
            charsLength += charName.matches("[^\\x00-\\xff]") ? 2 : 1;
            if (charsLength == maxLength) {
                return chars.substring(0, i + 1);
            }
            if (charsLength > maxLength) {
                return chars.substring(0, i);
            }
        }
        return chars;
    }

    public static int getStringLength(String chars) {
        if (Strings.isNullOrEmpty(chars)) {
            return 0;
        }
        int nameLength = 0;
        for (int i = 0; i < chars.length(); i++) {
            String charName = chars.substring(i, i + 1);
            // 判断中文 [\u4e00-\u9fa5]
            // 判断双字节字符 [^\\x00-\\xff]
            nameLength += charName.matches("[^\\x00-\\xff]") ? 2 : 1;
        }
        return nameLength;
    }

    public static List<String> splitterToArray(String sequence, String separator) {
        if (StrUtil.isEmpty(sequence)) {
            return null;
        }
        return Lists.newArrayList(Splitter.on(separator).omitEmptyStrings().split(sequence));
    }

    public static String joinerToString(List<String> arrays, String separator) {
        if (CollectionUtil.isEmpty(arrays)) {
            return null;
        }
        return Joiner.on(separator).skipNulls().join(arrays);
    }

    public static String getExt(String filename) {
        if (!Strings2.isNullOrWhiteSpace(filename)) {
            int index = filename.lastIndexOf(".");
            return filename.substring(index);
        }
        return "";
    }

    public static <T> String toJson(T t) {
        try {
            return new ObjectMapper().writeValueAsString(t);
        } catch (JsonProcessingException e) {
            return "";
        }
    }

    public static String trimHtmlLabel(String html) {
        return Jsoup.parse(html).text();
    }
}